﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using HsTec.DigitalPlatform.Business;
using HsTec.DigitalPlatform.DataAccess;
using HsTec.DigitalPlatform.Model.DTO;
using System.Reflection;

namespace HsTec.DigitalPlatform.WebSite.Controllers
{
    public class DeptController : Controller
    {
        DeptInfoManager deptinfomanager = new DeptInfoManager();
        //
        // GET: /Dept/

        public ActionResult Index()
        {
            return View();
        }
        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult GetJsonGridData()
        {
            int page = int.Parse(Request.Form["page"]);
            int rp = int.Parse(Request.Form["rp"]);
            string sortname = Request.Form["sortname"].ToString();
            string sortorder = Request.Form["sortorder"].ToString();

            List<DeptInfo> deptinfos = deptinfomanager.PageInfo(page, rp, sortname, sortorder).ToList<DeptInfo>();
            JsonFlexiGridData jsonflexgriddata = new JsonFlexiGridData();
            jsonflexgriddata.page = page;
            jsonflexgriddata.total = deptinfomanager.GetAllCount();
            if (jsonflexgriddata.rows == null)
            {
                jsonflexgriddata.rows = new List<FlexiGridRow>();
            }
            foreach (DeptInfo deptinfo in deptinfos)
            {
                FlexiGridRow row = new FlexiGridRow()
                {
                    id = deptinfo.deptid.ToString(),
                    cell = GetPropertyList(deptinfo)
                };
                jsonflexgriddata.rows.Add(row);
            }
            return Json(jsonflexgriddata);
        }
        private List<string> GetPropertyList(object obj)
        {
            List<string> propertyList = new List<string>();

            Type type = obj.GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo property in properties)
            {
                object o = property.GetValue(obj, null);
                propertyList.Add(o == null ? "" : o.ToString());
            }

            return propertyList;
        }

        [HttpGet]
        public ActionResult Add()
        {
            return PartialView();
        }

        [HttpPost]
        public ActionResult Add(FormCollection from)
        {
            DeptInfo deptinfo = new DeptInfo();
            try
            {
                deptinfo.deptname = from["deptname"];
                deptinfo.deptorder = int.Parse(from["deptorder"]);
                deptinfo.director = int.Parse(from["director"]);
                deptinfo.createuser = 0;
                deptinfo.createdate = DateTime.Now;
                deptinfomanager.Add(deptinfo);
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "新增失败！");
            }
            return PartialView();
        }
        [HttpPost]
        public ActionResult Edit(FormCollection from)
        {
            try
            {
                DeptInfo deptinfo = deptinfomanager.GetDeptInfoByID(int.Parse(from["deptid"]));
                deptinfo.deptname = from["deptname"];
                deptinfo.deptorder = int.Parse(from["deptorder"]);
                deptinfo.director = int.Parse(from["director"]);
                deptinfomanager.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("", "新增失败！");
            }
            return PartialView();
        }
        [HttpPost]
        public JsonResult Delete(string ids)
        {
            try
            {
                string[] ary = ids.Split(',');
                foreach (string id in ary)
                {
                    DeptInfo userinfo = deptinfomanager.GetDeptInfoByID(int.Parse(id));
                    deptinfomanager.Delete(userinfo);
                }
                return Json(new { msg = "删除成功！" });
            }
            catch
            {
                ModelState.AddModelError("", "删除失败！");
            }
            return Json(new { msg = "删除失败！" });
        }
        [HttpGet]
        public ActionResult View(int id)
        {
            return PartialView();
        }
    }
}
